

class DiamondClass {
    constructor(ops) {
        this.ctx = ops.ctx
        this.x = ops.x
        this.y = ops.y
        this.dir = ops.dir || 'down'
        this.row = ops.row || 0
        this.col = ops.col || 0
        this.diamondTime = ops.diamondTime || 1000
        this.diamondLazy = ops.diamondLazy || 0
        this.type = ops.type || 0
        this.diamondIndex = ops.diamondIndex || 0
        this.diamondObj = null
    }

    draw(){
        this.diamondObj = DiamondData[this.type][this.diamondIndex];
        for(let i = 0; i < 4;i++){
            for(let j = 0;j<this.diamondObj.diamonds.length;j++){
                if(this.diamondObj.diamonds[j][i] == 1){
                    Canvas.fillRect(GameManager.canvasList.diamond,i*25+5+this.x,j*25+5+this.y,15,15,'black');
                }
            }
        }
    }

    redenrDiamond (){
        if(this.diamondIndex == DiamondData[this.type].length-1){
            this.diamondIndex = 0;
        }else{
            this.diamondIndex += 1;
        }
    }
    
    update (){
        let position = this.calculateRowCol();

        if(!this.checkDown(position)){
            return;
        }

        if(this.diamondLazy > 0){
            this.diamondLazy -= GameManager.timeInterval;
            return false;
        }else{
            this.diamondLazy = this.diamondTime;
        }

        //移动
        switch(this.dir){
            case "left":
                this.checkLeft(position);
                break;
            case "right":
                this.checkRight(position);
                break;
            default:
                break;
        }
        this.y += 25;
        this.dir = 'down';
    }
    
    checkLeft (position){
        if (position.col <= 0){
            return;
        }

        position.col -= 1;

        let canExec = true;

        for (let i = 0; i < this.diamondObj.diamonds.length; i++) {
            for (let j = 0; j < 4; j++) {
                let extRow = position.row + i;
                let extCol = position.col + j;
                if (this.diamondObj.diamonds[i][j] != 0 && BackgroundHelper.mapCode[extRow][extCol] !== 0) {
                    canExec = false;
                    break;
                }
            }
        }

        if (!canExec){
            return;
        }

        this.x -= 25;
    }
    
    checkRight (position){
        if (position.col >= GameManager.col -1){
            return;
        }

        if (position.col + this.diamondObj.width > GameManager.col - 1){
            return;
        }
        position.col += 1;
        let canExec = true;
        for (let i = 0; i < this.diamondObj.diamonds.length; i++) {
            for (let j = 0; j < 4; j++) {
                let extRow = position.row + i;
                let extCol = position.col + j;
                if (this.diamondObj.diamonds[i][j] != 0 && BackgroundHelper.mapCode[extRow][extCol] !== 0) {
                    canExec = false;
                    break;
                }
            }
        }

        if (!canExec){
            return;
        }

        this.x += 25;
    }
    
    checkDown (position){
        if (this.check(position.row, position.col)) {
            return true;
        } else {
            GameManager.diamondList.remove(this);
            this.renderMap();
            GameManager.initDiamond();
            return false;
        }
    }
    
    check (row,col){
        if(row >= GameManager.row-1){
            return false;
        }
        if(row + this.diamondObj.height > GameManager.row - 1){
            return false;
        }
        //下探一行
        row += 1;
        for (let i = 0; i < this.diamondObj.diamonds.length; i++) {
            for (let j = 0; j < 4; j++) {
                let extRow = row + i;
                if (this.diamondObj.diamonds[i][j] != 0 && BackgroundHelper.mapCode[extRow][col + j] !== 0) {
                    return false;
                }
            }
        }
        return true;
    }
    
    calculateRowCol (){
        this.row = this.y / 25;
        this.col = this.x / 25;
        return {"row":this.row,"col":this.col};
    }
    
    renderMap (){
        for (let i = 0; i < this.diamondObj.diamonds.length; i++) {
            for (let j = 0; j < 4; j++) {
                //   将现在已有的方块渲染到Map类的mapCode上
                if (this.diamondObj.diamonds[i][j] !== 0) {
                    let extRow = this.row + i;
                    if(extRow>GameManager.row-1){
                        extRow = GameManager.row-1;
                    }
                    let extCol = this.col + j;
                    if(extCol>GameManager.col-1){
                        extCol = GameManager.col-1;
                    }
                    // 改变地图的mapCode数据
                    BackgroundHelper.mapCode[extRow][extCol] = this.diamondObj.diamonds[i][j];
                }
            }
        }
    }
}

export default DiamondClass